<template>
	<div id="vmDiskPart" class="height100">
		<div class="tableCon" style="height:230px">
			<ics-el-table
				ref="disksTable"
				:config="diskTableConfig"
				:localData="diskTableConfig.localTableData"
				@selectRow="selectRow"
				@loadComplete="loadComplete">
				<templete slot-scope="props">
					<ul class="table-operate">
						<li @click="importQCow" title="选择镜像">
							<span class="icon-qcow2"></span>
						</li>
					</ul>
				</templete>
			</ics-el-table>
		</div>
		<div style="border-top: 1px solid #ddd;padding-top: 10px;height: calc(100% - 230px);overflow-y: auto;">
			<el-form ref="diskForm" v-model="vmDisk" label-width:"100px" v-if="disk">
				<div>
					<el-form-item label="磁盘大小">
						<el-input-item v-model="disk.volume.size" ref="diskSize"
							:disabled="diskSizeDisabled" controls-position="position"
							name="diskSize" data-type="input" v-validate-easy="rules.diskSize"
							@change="sizeChange">
						</el-input-item> GB
						<button class="m-size" type="button" @click="editDiskQos" v-if="modetype !== 'importVm'">
							编辑磁盘QoS
						</button>
					</el-form-item>
					<el-form-item label="" v-if="modetype !== 'importVm'">
						<el-checkbox v-model="disk.volume.shared" :disabled="shareDisabled" @change="changeShared">
							是否共享
						</el-checkbox>
						<el-checkbox v-model="disk.volume.bootable" :disabled="bootDisabled" @change="changeBootAble">
							是否作为引导盘
						</el-checkbox>
						<el-checkbox v-model="disk.volume.formatDisk" v-if="type === 1">是否格式化</el-checkbox>
					</el-form-item>
					<el-form-item label="磁盘策略" v-if="modetype !== 'importVm'">
						<el-select v-model="disk.volume.volumePolicy" :disabled="volumePolicyDisabled">
							<el-option v-for="(item, index) in policyArray"
								:key="index"
								:value="item.value"
								:label="item.text">
							</el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="磁盘格式" v-if="modetype !== 'importVm'">
						<el-select v-model="disk.volume.format"
							:disabled="formatDisabled"
							@change="formatChange">
							<el-option v-for="(type, index) in vdiskStorageTypeArray"
								:key="index"
								:value="type.value"
								:label="type.text"
								:disabled="type.disabled">
							</el-option>
						</el-select>
					</el-form-item>
				</div>
			</el-form>
		</div>
	</div>
</template>

<script>
export default {
	name: 'disk',
	props: {
		vmData: {},
		modetype: {},
		hostData: {},
		options: {},
		addNew: {default: false}
	},
	data() {
		return {
			ref: 'DiskPart',
			vmDisk: {
				disks: []
			},
			diskTableConfig: {
				mode: '',
				showRadio: true,
				localTableData: [],
				operateWidth: 55,
				ttt: false,
				columns: this.getDiskTableCol(),
				beforeSelect: this.beforeSelect
			},
			type: 0,
			disk: null,
			diskIndex: 0,
			oldDiskIndex: 0,
			activeDiskIndex: 0,
			storeInfo: null,
			snapInfo: {
				hasMemSnap: null,
				hasSnap: null
			},
			l2CacheSizeShow: this.disk ? (this.disk.l2CacheSize ? this.disk.l2CacheSize / 1024 / 1024 : '') : '',
			enableKernelIOTip: '开启内核IO加速的磁盘暂不支持读写速率监控。',
			busModeTip: '1 VIRTIO类型的磁盘不支持创建GPT分区;<br/>2 最多添加3块IDE磁盘;<br/>3 切换磁盘接口可能造成虚拟机操作系统内盘符变化,导致磁盘不可用'
		};
	},
	watch: {
		guestosType: function() {
			this.changeNativeIO();
		},
		'options': {
			handler(val) {
				if (val && val.currentTabName === 'DiskPart') {
					if (val.addNew) {
						this.addDisk(val.type);
					} else if (val.delOne) {
						this.delDisk();
						this.$emit('changeAddOrDel', 'delOne', false);
					}
				}
			},
			deep: true
		},
		// 虚拟机信息修改后，回传信息给父组件
		'vmDisk.disks': {
			handler() {
				this.setTableData();
				this.$emit('updateVmDate', 'DiskPart', this.vmDisk.disks);
			},
			deep: true
		},
		'vmData.version'(val) {
			if (val === 'V1') {
				this.vmDisk.disks = [];
				this.addNewDisk();
			}
		}
	},
	computed: {
		defaultDisk() {
			let defaultOptions = {
				volume: {
					bootable: false,
					clusterSize: 262144,
					dataStoreName: this.vmData.dataStoreName,
					dataStoreId: this.vmData.dataStoreId,
					dataStoreSize: this.vmData.dataStoreSize,
					dataStoreType: this.vmData.dataStoreType,
					format: this.vmData.version === this.enums.VmVersionEnum.V1 ? 'QCOW2' : 'RAW',
					shared: false,
					size: 40,
					formatDisk: false,
					volumePolicy: this.enums.VDiskEnum.THIN
				},
				queueNum: 1,
				busModel: 'IDE',
				enableNativeIO: false,

				enableKernelIO: false,
				l2CacheSize: '',
				readWriteModel: 'NONE',
				totalIops: '0',
				readIops: '0',
				writeIops: '0',
				totalBps: '0',
				readBps: '0',
				writeBps: '0',
				type: 0
			};
			if (!this.vmData.id && this.diskIndex === 0) {
				if (this.vmData.guestosType && this.vmData.guestosType.indexOf('Windows') > -1) {
					defaultOptions.busModel = this.enums.VMBusModelEnum.IDE;
				} else {
					defaultOptions.busModel = this.enums.VMBusModelEnum.VIRTIO;
				}
			} else {
				defaultOptions.busModel = this.enums.VMBusModelEnum.VIRTIO;
			}
			return defaultOptions;
		}
	},
	created() {
		let that = this;
		if (this.vmData && this.vmData.disks) {
			this.vmData.disks.forEach(x => {
				x.type = 0;
				x.diskIndex = ++that.diskIndex;
				x.volumec.minSize = x.volume.size;
				that.vmDisk.disks.push(x);
			});
			that.oldDiskIndex = this.diskIndex;
			this.setTableData();
			this.disk = this.vmDisk.disks[this.vmDisk.disks.length - 1];
		}
		if (this.vmData.id) {
			this.getSnapInfo();
		}
	},
	mounted() {
		if (this.modetype === 'addVm' || (this.options && this.options.addNew)) {
			this.addDisk(this.options.type);
			this.$emit('updateVmDate', 'DiskPart', this.vmDisk.disks);
			this.$emit('changeAddOrDel', 'addNew', false);
		}
		this.activeDiskIndex = this.vmDisk.disks.length - 1;
	},
	updated() {
	},
	methods: {
		addNewDisk() {
			let defaultDisk = JSON.parse(JSON.stringify(this.defaultDisk));
			defaultDisk.diskIndex = ++this.diskIndex;
			defaultDisk.volume.bootable = defaultDisk.diskIndex !== 1;
		}
	}
};
</script>

<style>
</style>
